home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
src
/
fig07_06.jar
/
Ch07
/
Fig07_06
/
Fig07_06.cpp
next >
Wrap
C/C++ Source or Header
|
1997-10-20
|
659b
|
31 lines
// Fig. 7.6: fig07_06.cpp
// Non-friend/non-member functions cannot access
// private data of a class.
#include <iostream.h>
// Modified Count class
class Count {
public:
Count() { x = 0; } // constructor
void print() const { cout << x << endl; } // output
private:
int x; // data member
};
// Function tries to modify private data of Count,
// but cannot because it is not a friend of Count.
void cannotSetX( Count &c, int val )
{
c.x = val; // ERROR: 'Count::x' is not accessible
}
int main()
{
Count counter;
cannotSetX( counter, 3 ); // cannotSetX is not a friend
return 0;
}